var Post = require('../handlers/postHandler')
var User = require('../handlers/userHandler')
var util = require('../utility/util')

module.exports = {

  getPostList: function(req, res) {
    var query = req.query
    var page = req.params.page
    var filter = null
    if (query && query.userId) {
      filter = {
        userId: query.userId
      }
    }
    Post.find(page, filter, function(err, posts) {
      if (err) {
        console.log('postController getPostList fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      if (posts.length == 0) {
        return res.json({
          code: 112,
          msg: '没有数据'
        })
      }
      return res.json({
        code: 100,
        msg: '获取成功',
        data: {
          list: posts
        }
      })
    })

  },
  getOne: function(req, res) {
    var id = req.params.id

    Post.findById(id, function(err, post) {
      if (err) {
        console.log('postController getOne fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      if (!post) {
        return res.json({
          code: 112,
          msg: '没有数据'
        })
      }
      return res.json({
        code: 100,
        msg: '获取成功',
        data: {
          post: post
        }
      })
    })

  },
  add: function(req, res) {
    var body = req.body
    Post.insert(body, function(err, post) {
      if (err) {
        console.log('postController add fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      return res.json({
        code: 100,
        msg: '发表成功',
        data: {
          post: post
        }
      })
    })
  },
  remove: function(req, res) {
    var id = req.params.id

  },
  addViewport: function(req, res) {
    var id = req.params.id
    var body = req.body

    Post.findByIdAndUpdate(id, body, function(err, post) {
      if (err) {
        console.log('postController addViewport fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      return res.json({
        code: 100,
        msg: '操作成功！',
        data: {
          post: post
        }
      })
    })
  },
  addCollect: function(req, res) {
    var id = req.params.id
    var handlerId = req.body.handlerId

    Post.findByIdAndUpdate(id, {
      handlerId: handlerId
    }, function(err, post) {
      if (err) {
        console.log('postController addCollect fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }

      User.findByIdAndUpdate(handlerId, {
        handlerType: 'addCollect',
        content: {
          postId: id
        }
      }, function(err, user) {
        if (err) {
          console.log('postController addCollect fail. err = ' + err)
          return res.json({
            code: 177,
            msg: err
          })
        }
        return res.json({
          code: 100,
          msg: '操作成功',
          data: {
            post: post
          }
        })
      })
    })

  }

}